<?php

namespace App\Repository\Sms;

use App\Models\Sms\SmsSendLogModel as model;
use App\Repository\BaseRepository;
use App\Repository\Order\OrderRepository;
use App\Repository\Product\GoodsRepository;
use App\Repository\Product\ProductRepository;
use App\Repository\Shop\SuppilerRepository as supp;

class SmsSendLogRepository extends BaseRepository
{
    private $model;
    private $productModel;
    private $goodsModel;
    private $smsTmp;
    private $orderModel;
    private $suppModel;

    public function __construct()
    {
        $this->model = new model;
        $this->productModel = new ProductRepository;
        $this->goodsModel = new GoodsRepository;
        $this->smsTmp = new SmsTmpRepository;
        $this->orderModel = new OrderRepository;
        $this->suppModel = new supp;
        parent::__construct();
    }

    //列表
    public function index($where)
    {
        $cron = $where;
        $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
        // $cron['where'] = $where;
        // var_dump($cron,111111111);

        $data = $this->search($this->model, $cron, 1, 9000000);
        if (!empty($data['data'])) {
            $data['data'] = usfirstArr($data['data']);
        }
        return $data;
    }

    //添加
    public function add($data)
    {
        //必填 字段
        $mustField = [
            'sms_id',
            'sms_tmp_id',
            // 'order_status',
            'order_id'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'sms_id' => '发送信息提交返回的smsId不能为空',
            'sms_tmp_id' => '模板不能为空',
            // 'order_status' => '订单状态不能为空',
            'order_id' => '订单号不能为空'
        ];
        $insertData = $this->getRealData($data, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
        if (empty($insertData['delay'])) {
            $insertData['delay'] = 0;
        }
        if (empty($data['shop_id'])) {
            if ($insertData['sms_tmp_id'] != 1) {
                return $this->returnMsg(4, '店铺不能为空');
            }
            $data['shop_id'] = 1;
        }
        $isTrue = $this->getOne(['sms_tmp_id' => $insertData['sms_tmp_id'], 'delay' => $insertData['delay'], 'shop_id' => $data['shop_id'], 'order_id' => $data['order_id'], 'status' => 0]);
        if ($isTrue && !empty($insertData['content']) && $isTrue->content == $insertData['content']) {
            return $this->returnMsg(3, '该短信已发送');
        }
        $insertData['shop_id'] = !empty($data['shop_id']) ? $data['shop_id'] : '';

        $insertData['created_at'] = date('Y-m-d H:i:s');
        // $insertData['created_by'] = $data->user->id;
        // $insertData['created_by_name'] = $data->user->name;

        $res = $this->model->insertGetId($insertData);
        if ($res) {
            return $this->returnMsg(200, '添加成功', ['id' => $res]);
        }
        return $this->returnMsg(1, '添加失败');
    }

    //删除
    public function del($where)
    {
        $flag = true;
        $msg = '未知错误';
        $code = 1;
        if (empty($where)) {
            $flag = false;
            $msg = '删除条件不能为空';
            $code = 2;
        }
        if ($flag) {
            $res = $this->model->where($where)->get();
            if (!$res) {
                $flag = false;
                $msg = '未查询到数据';
                $code = 3;
            }
        }
        if ($flag) {
            $del = $this->model->where($where)->delete();
            if ($del) {
                $msg = '删除成功';
                $code = 200;
            }
        }
        return $this->returnMsg($code, $msg);
    }

    //修改
    public function edit($where, $data, $isMust = false)
    {
        if (empty($where)) {
            return $this->returnMsg(1, '条件不能为空');
        }
        if (empty($data)) {
            return $this->returnMsg(2, '修改参数不能为空');
        }
        //过滤字段
        //过滤字段
        $optionalFields = $this->model->getTableField();
        $field = $this->getRealData($data, [], $optionalFields, []);

        //先查询是否存在
        // $model = $this->model;
        // $isWhere = false;
        // if (!empty($where['in'])) {
        //   unset($where['in']);
        //   $isWhere = true;
        //   $model = $model->whereIn($where['in']['field'], $where['in']['value']);
        // }
        // if (!empty($where['select'])) {
        //   $isWhere = true;
        //   unset($where['select']);
        //   $model = $model->where($where['select']);
        // }
        // if (!$isWhere) {
        //   return $this->returnMsg(100, '修改条件不能为空');
        // }
        // DB::connection()->enableQueryLog();
        if (empty($where['select']) && empty($where['in'])) {
            $this->model = $this->model->where($where);
        }

        if (!empty($where['select'])) {
            $this->model = $this->model->where($where['select']);
        }
        if (!empty($where['in'])) {
            $this->model = $this->model->whereIn($where['in']['field'], $where['in']['value']);
        }
        $res = $this->model->update($field);
        // var_dump($res, 11111111);
        if ($isMust && $res) {
            return $this->returnMsg(200, '修改成功');
        }
        if (!$isMust) {
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(3, '请重新修改');
    }

    //列表查询
    public function lists($where = [], $page = 1, $limit = 10)
    {
        $res = $this->search($this->model, $where, $page, $limit);

        return $res;
    }

    public function getOne($where, $field = ['*'])
    {
        return $this->model->where($where)->select($field)->orderBy('id', 'desc')->first();
    }
}
